package cn.gailvlun.gll.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.databinding.BindingAdapter;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.gailvlun.gll.R;



public class CountEditView extends LinearLayout {
    public CountEditView(Context context) {
        this(context, null);
    }

    public CountEditView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CountEditView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CountEditView);
        mCountAmount = ta.getInteger(R.styleable.CountEditView_count_amount, 500);
        mHint = ta.getString(R.styleable.CountEditView_hint);
        mHintColor = ta.getColor(R.styleable.CountEditView_hint_color,
                getResources().getColor(R.color.warmGrey));
        editTextColor = ta.getColor(R.styleable.CountEditView_edit_text_color,
                getResources().getColor(R.color.brownishGrey));
        ta.recycle();

        init();
    }

    private Context mContext;

    private int mCountAmount;
    private CharSequence mHint;
    private int mHintColor;
    private int editTextColor;
    private EditText etContent;
    private TextView tvCount;

    private void init() {
        setOrientation(VERTICAL);

        inflate(mContext, R.layout.view_count_edit, this);

        etContent = findViewById(R.id.et_content);
        tvCount = findViewById(R.id.tv_count);

        InputFilter[] filters = {new InputFilter.LengthFilter(mCountAmount)};
        etContent.setFilters(filters);
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                int used = s.length();
                tvCount.setText(used + "/" + mCountAmount);
            }
        });

        etContent.setHintTextColor(mHintColor);
        etContent.setHint(mHint);
        etContent.setTextColor(editTextColor);
        tvCount.setText("0/" + mCountAmount);
    }

    public String getText() {
        return etContent.getText().toString();
    }

    public EditText getEditText() {
        return etContent;
    }

    public TextView getCountTextView() {
        return tvCount;
    }

    public void setHint(CharSequence hint) {
        etContent.setHint(hint);
    }

    public void setText(String content) {
        etContent.setText(content);
        etContent.setSelection(etContent.getText().length());

    }

    @BindingAdapter("app:text")
    public static void setText(CountEditView ctv, String content) {
        if (ctv != null) {
            ctv.setText(content);
        }
    }

    public void appendText(String text) {
        int index = etContent.getSelectionStart();
        Editable edit = etContent.getEditableText();
        if (index < 0 || index >= edit.length()) {
            edit.append(text);
        } else {
            edit.insert(index, text);
        }
    }
}
